var call = {
    "r_data" : {},
    "state" : "",
    "state_C" : "",
    "stateremark" : "",
    "acdcount" : "",
    "IP" : "",
    "sipnum" : "",
    "cmdport" : "7100",
    "statport" : "7200",
    "evaluate_luaname" : "dddmydpj",
    "evaluate_remark" : "123456789",
    "interval" : window.setInterval("",500),
    "setStatus" : function(){
        var url = "http://"+this.IP+":"+this.statport+"/stat.html?cmd=08&format=json&groupid=-1&sipnum="+this.sipnum;
        this.getData(url);
        var getState = this.r_data;
        if(getState!=null&&getState.call!=null&&getState.call.length>0){
            var g_state = getState.call[0].state;
            var stateremark = getState.call[0].stateremark;
            var acdcount = getState.call[0].acdcount;
            if(g_state!=null){
                if(g_state!=this.state||stateremark!=this.stateremark||acdcount!=this.acdcount){
                    this.state = g_state;
                    this.stateremark = stateremark;
                    this.acdcount = acdcount;
                    this.state_C = this.setState(g_state,stateremark);
                    this.setStatusData();
                }
            }
        }else{
            this.LinkEvent_OnDisconnected(this.sipnum);
            /*clearInterval(this.interval);*/
        }
    },
    "setStatusData" : function(){},
    "init" : function(IP,sipnum){
        this.IP = IP;
        this.sipnum = sipnum;
    },
    "getLoginUrl" : function(jobnum,jobname,groupnum,groupname){
        return "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=09&format=json&" +
                "sipnum="+this.sipnum+"&state=login&" +
                "jobnum="+jobnum+"&jobname="+encodeURI(jobname)+"&groupnum="+groupnum+"&groupname="+encodeURI(groupname);
    },
    "login" : function(IP,sipnum,jobnum,jobname,groupnum,groupname){
        this.IP = IP;
        this.sipnum = sipnum;
        this.state = "";
        this.interval = window.setInterval("call.setStatus()",500);
        var url = this.getLoginUrl(jobnum,jobname,groupnum,groupname);
        return this.getData(url,this.loginSucc,this.loginError);
    },
    "loginSucc" : function(){},
    "loginError" : function(){},
    "getLogoutUrl" : function(){
        return "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=09&format=json&sipnum="+this.sipnum+"&state=logout";
    },
    "logout" : function(){
        var url = this.getLogoutUrl();
        clearInterval(this.interval);
        this.getData(url,this.logoutSucc,this.logoutError);
    },
    "logoutSucc" : function(){},
    "logoutError" : function(){},
    "busy" : function(){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=09&format=json&sipnum="+this.sipnum+"&state=busy";
        this.getData(url,this.busySucc,this.busyError);
    },
    "busySucc" : function(){},
    "busyError" : function(){},
    "idle" : function(){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=09&format=json&sipnum="+this.sipnum+"&state=idle";
        this.getData(url,this.idleSucc,this.idleError);
    },
    "idleSucc" : function(){},
    "idleError" : function(){},
    "rest" : function(){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=09&format=json&sipnum="+this.sipnum+"&state=busy&remark="+encodeURI("休息");
        this.getData(url,this.restSucc,this.restError);
    },
    "restSucc" : function(){},
    "restError" : function(){},
    "eat" : function(){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=09&format=json&sipnum="+this.sipnum+"&state=busy&remark="+encodeURI("吃饭");
        this.getData(url,this.eatSucc,this.eatError);
    },
    "eatSucc" : function(){},
    "eatError" : function(){},
    "training" : function(){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=09&format=json&sipnum="+this.sipnum+"&state=busy&remark="+encodeURI("培训");
        this.getData(url,this.trainingSucc,this.trainingError);
    },
    "trainingSucc" : function(){},
    "trainingError" : function(){},
    "callout" : function(destnum,outshowtel,calloid){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=07&format=json&sipnum="+this.sipnum+"&" +
                "destnum="+destnum+"&outshowtel="+outshowtel+"&calloid="+calloid;
        this.getData(url,this.calloutSucc,this.calloutError);
    },
    "calloutSucc" : function(){},
    "calloutError" : function(){},
    "callbreak" : function(){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=03&format=json&sipnum="+this.sipnum;
        this.getData(url,this.callbreakSucc,this.callbreakError);
    },
    "callbreakSucc" : function(){},
    "callbreakError" : function(){},
    "hold" : function(){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=04&format=json&sipnum="+this.sipnum;
        this.getData(url,this.holdSucc,this.holdError);
    },
    "holdSucc" : function(){},
    "holdError" : function(){},
    "restore" : function(){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=05&format=json&sipnum="+this.sipnum;
        this.getData(url,this.restoreSucc,this.restoreError);
    },
    "restoreSucc" : function(){},
    "restoreError" : function(){},
    "evaluate" : function(evaluate_luaname,evaluate_remark){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=15&format=json&sipnum="+this.sipnum+"&lua="+evaluate_luaname+"&remark="+evaluate_remark;
        this.getData(url,this.evaluateSucc,this.evaluateError);
    },
    "evaluateSucc" : function(){},
    "evaluateError" : function(){},
    "transfer" : function(destnum){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=06&format=json&sipnum="+this.sipnum+"&destnum="+destnum;
        this.getData(url,this.transferSucc,this.transferError);
    },
    "transferSucc" : function(){},
    "transferError" : function(){},
    "meeting" : function(destnum,outshowtel){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=14&format=json&sipnum="+this.sipnum+"&destnum="+destnum+"&outshowtel="+outshowtel;
        this.getData(url,this.transferSucc,this.transferError);
    },
    "meetingSucc" : function(){},
    "meetingError" : function(){},
    "listen" : function(sipnum,destnum){
        var url = "http://"+this.IP+":"+this.cmdport+"/ipcc.html?cmd=10&format=json&sipnum="+sipnum+"&destnum="+destnum;
        this.getData(url,this.listenSucc,this.listenError);
    },
    "listenSucc" : function(){},
    "listenError" : function(){},
    /*来电时的调用函数*/
    "ExtEvent_OnCallIn": function(sipnum,direction,callerid,uuid,createdtime,hoteline,dtmfMy,dtmfDest,acdcount){
        console.log("来电了");
    },
    /*通话时的调用函数*/
    "ExtEvent_OnConnected": function(sipnum,direction,callerid,uuid,createdtime,hoteline,dtmfMy,dtmfDest,acdcount){
        console.log("通话了");
    },
    /*挂机时的调用函数*/
    "ExtEvent_OnDisconnected": function(sipnum,direction,callerid,uuid,createdtime,hoteline,dtmfMy,dtmfDest,acdcount){
        console.log("挂机了");
    },
    /*外呼结束时的调用函数*/
    "ExtEvent_OnCallFinished": function(sipnum,direction,callerid,uuid,createdtime,hoteline,dtmfMy,dtmfDest,acdcount){
        console.log("外呼结束了");
    },
    /*连接断开时的调用函数*/
    "LinkEvent_OnDisconnected": function(sipnum){
        console.log(sipnum+"断开了");
    },

    "getData" : function(url,succ,error){
        console.log(url);
        var that = this;
        var xhr1 = $.ajax({
            url: url+"&callback=?",
            type:"get",
            dataType: "jsonp",
            jsonp: 'callback',
            timeout: 5000,
            success:function(data){
                that.r_data = eval(data);
                if(succ){
                    succ();
                }
            },error:function(data){
                console.log("ajax错误信息："+data);
                that.r_data = {};
                if(error){
                    error();
                }
            }
        });
    },
    "setState" : function(state,stateremark,isFunc){
        var data = null;
        if(!isFunc){/*判断是否执行函数，默认执行*/
            data = this.r_data.call[0];
        }
        switch(state){
            case 'logout':
                return "签出";
                break;
            case 'idle':
                return "空闲";
                break;
            case 'busy':
                if(stateremark!=null){
                    if(stateremark=="吃饭"){
                        return "吃饭";
                    }else if(stateremark=="休息"){
                        return "休息";
                    }else if(stateremark=="培训"){
                        return "培训";
                    }else{
                        return "置忙";
                    }
                }
                break;
            case 'after':
                if(data){
                    this.ExtEvent_OnDisconnected(this.sipnum,data.direction,data.callerid,data.uuid,data.createdtime,data.hoteline,data["dtmf-my"],data["dtmf-dest"],data.acdcount);
                }
                return "话后";
                break;
            case 'ringing':
                if(data){
                    this.ExtEvent_OnCallIn(this.sipnum,data.direction,data.callerid,data.uuid,data.createdtime,data.hoteline,data["dtmf-my"],data["dtmf-dest"],data.acdcount);
                }
                return "振铃";
                break;
            case 'answered':
                if(data){
                    this.ExtEvent_OnConnected(this.sipnum,data.direction,data.callerid,data.uuid,data.createdtime,data.hoteline,data["dtmf-my"],data["dtmf-dest"],data.acdcount);
                }
                return "通话";
                break;
            case 'hold':
                return "保持";
                break;
            default:
                return "签出";
        }
    }
};
